class GomokuGame:
    def __init__(self, size=15):
        self.size = size  # 棋盘大小
        self.board = [['.' for _ in range(size)] for _ in range(size)]  # 初始化棋盘
        self.current_player = 'X'  # 当前玩家
        self.game_over = False
        self.winner = None

    def print_board(self):
        """打印棋盘"""
        # 打印列号
        print("  " + " ".join([f"{i:2}" for i in range(self.size)]))

        for i, row in enumerate(self.board):
            # 打印行号和棋盘内容
            print(f"  {i:2} " + "  ".join(row))

    def is_valid_move(self, row, col):
        """检查落子位置是否有效"""
        if row < 0 or row >= self.size or col < 0 or col >= self.size:
            return False
        return self.board[row][col] == '.'

    def make_move(self, row, col):
        """玩家落子"""
        if self.game_over:
            print("游戏已经结束！")
            return False

        if not self.is_valid_move(row, col):
            print("无效的位置！请重新选择。")
            return False

        self.board[row][col] = self.current_player
        if self.check_win(row, col):
            self.game_over = True
            self.winner = self.current_player
            return True

        # 切换玩家
        self.current_player = 'O' if self.current_player == 'X' else 'X'
        return True

    def check_win(self, row, col):
        """检查是否获胜"""
        player = self.board[row][col]

        # 检查方向：水平、垂直、两个对角线
        directions = [(0, 1), (1, 0), (1, 1), (1, -1)]

        for dr, dc in directions:
            count = 1  # 当前位置已经有一个棋子

            # 正向检查
            r, c = row + dr, col + dc
            while 0 <= r < self.size and 0 <= c < self.size and self.board[r][c] == player:
                count += 1
                r += dr
                c += dc

            # 反向检查
            r, c = row - dr, col - dc
            while 0 <= r < self.size and 0 <= c < self.size and self.board[r][c] == player:
                count += 1
                r -= dr
                c -= dc

            # 如果有5个连续的棋子，获胜
            if count >= 5:
                return True

        return False

    def play(self):
        """开始游戏"""
        print("欢迎来到五子棋游戏！")
        print("玩家 X 和 O 轮流下棋")
        print("输入行号和列号（0-14）来放置棋子")

        while not self.game_over:
            print("\n" + "=" * 30)
            self.print_board()
            print(f"\n玩家 {self.current_player} 的回合")

            try:
                row = int(input("请输入行号: "))
                col = int(input("请输入列号: "))

                if not self.make_move(row, col):
                    continue

            except ValueError:
                print("请输入有效的数字！")
                continue

        # 游戏结束
        print("\n" + "=" * 30)
        self.print_board()
        print(f"\n游戏结束！玩家 {self.winner} 获胜！")
        print("=" * 30)


# 开始游戏
if __name__ == "__main__":
    game = GomokuGame()
    game.play()